<template>
  <div class="container">
    <div class="header_box">
      <p style="padding:13px;">
        当前机构调价权：
        <span>{{this.masterData.operateRatio}}</span>
      </p>
    </div>
    <el-form ref="form" :model="form" label-width="120px" style="width:100%;padding-top:20px;">
      <el-form-item label="折扣状态：">
        <el-radio-group v-model="form.resource"></el-radio-group>
        <el-radio v-model="radios" label="1" @change="radioChange">开启</el-radio>
        <el-radio v-model="radios" label="0" @change="radioChange">关闭</el-radio>
      </el-form-item>
      <el-form-item label="设置折扣区间：" v-if="this.radios == 1">
        <el-input style="width:8%" placeholder="最小值" type="number" v-model="form.minRatio"></el-input>--
        <el-input style="width:8%" placeholder="最大值" type="number" v-model="form.maxRatio"></el-input>
        <span>({{this.masterData.operateRatio}} ~ 1) 保留小数点后2位</span>
      </el-form-item>
      <el-form-item v-else></el-form-item>
      <el-form-item>
        <div style="margin-left:-110px;color:rgb(148,167,244)">注：折扣区间一旦设置并开启，商品订单页可根据当前设置选择折扣幅度</div>
      </el-form-item>
      <el-form-item>
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="Preservation">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      masterData: [],
      form: {
        minRatio: "",
        maxRatio: ""
      },
      radios: "1",
      rules: {}
      // closeStatus  0 关闭   1  开启
    };
  },
  created() {
    this.getData();
  },
  methods: {
    cancel() {
      this.$router.push({ path: "/Dashboard" });
    },
    radioChange(number) {
      console.log(number);
      this.radios = number;
      // number == "开启" ?this.radio = 1 :this.radio = 0
    },
    //获取机构信息
    getData() {
      var that = this;
      let organizationId = JSON.parse(
        localStorage.getItem("organizationIds")
      )[0];
      this.url = "/api/user/xzs/organization.info.get";
      this.$axios({
        url: this.url,
        method: "post",
        data: {
          organizationId: organizationId
        },
        transformRequest: [
          function(data) {
            let ret = "";
            for (let it in data) {
              ret +=
                encodeURIComponent(it) +
                "=" +
                encodeURIComponent(data[it]) +
                "&";
            }
            return ret;
          }
        ]
      })
        .then(res => {
          this.masterData = res.data.data;
          if (this.masterData.maxRatio === undefined) {
            this.radios = "0";
            // this.form.maxRatio = undefined;
            // this.form.minRatio = undefined;
          } else {
            this.radios = "1";
          }
          this.form = Object.assign({}, this.form, res.data.data);
        })
        .catch(err => {});
    },
    //  折扣保存
    AxiosPreservation(organizationId) {
      var that = this;
      this.$axios({
        url: this.url,
        method: "post",
        data: {
          closeStatus: that.radios,
          organizationId: organizationId,
          maxRatio: that.form.maxRatio,
          minRatio: that.form.minRatio
        },
        transformRequest: [
          function(data) {
            let ret = "";
            for (let it in data) {
              ret +=
                encodeURIComponent(it) +
                "=" +
                encodeURIComponent(data[it]) +
                "&";
            }
            return ret;
          }
        ]
      }).then(res => {
        if(res.data.errorCode == 0){
          this.$message({
            showClose: true,
            message: "设置成功",
            type: "success"
          });
        }
      });
    },
    Preservation() {
      this.url = "/api/user/xzs/organization.info.update";
      let organizationId = JSON.parse(localStorage.getItem("organizationIds"))[0];
      if (this.radios == "1") {
        if (this.form.maxRatio == "" || this.form.minRatio == "") {
          this.$message({
            showClose: true,
            message: "请设置折扣区间",
            type: "warning"
          });
        } else if(parseFloat(this.form.minRatio) < this.masterData.operateRatio || parseInt(this.form.maxRatio) > 1) {
          this.$message({
            showClose: true,
            message:"折扣价格只能设置在" + this.masterData.operateRatio + "-" + 1 + "之间",
            type: "warning"
          });
        } else {
          this.AxiosPreservation(organizationId);
        }
      }else{
          this.AxiosPreservation(organizationId);
      }
    }
  }
};
</script>

<style>
.header_box {
  width: 100%;
  height: 50px;
  margin: -10px 0 0 0;
  background: rgb(242, 242, 242);
}
</style>
